console.log('-----1.简单工厂模式-------')
//1.1 简单工厂模式
//咖啡类的抽象类基类
abstract class Coffe {
  constructor(public name:string) {
    this.getCoffe(this.name);
  };
  getCoffe(name: string): void{
    console.log('要制作的咖啡是：', name);
  }
};

class AmriCoffe extends Coffe {
  constructor(name:string) {
    super(name);
  };
}
class LatteCoffe extends Coffe {
  constructor(name:string) {
    super(name);
  };
}
class FlowerCoffe extends Coffe {
  constructor(name:string) {
    super(name);
  };
};

// 简单工厂类
class FactorySimple {
  constructor(name:string) {
    console.log('顾客要制作的coffe是=》',name)
  };
  static order(coffeName: string): any{
    switch (coffeName) {
      case '美式咖啡':
        return new AmriCoffe(coffeName);
        break;
      case '拿铁咖啡':
        return new LatteCoffe(coffeName);
        break;
      case '花式咖啡':
        return new FlowerCoffe(coffeName);
        break;
      default:
        return 'sorry!我们这里没有这种咖啡！'
    }
  }
};

// 测试简单工厂模式
let factorySimpel = new FactorySimple('111');
let res = FactorySimple.order('拿铁咖啡')
console.log(res);



//1.2 简单工厂模式用例：以实现jquery的html（）方法为例子
interface Window{
  $:any
}
window.$ = function (selector: string) {
  return new JQuery(selector);
};
// 简单实现一个JQuery类
interface JQuery{
  [index:number]:any
}
class JQuery {
  length: number;
  constructor(selector:string) {
    let elements = [].slice.call(document.querySelectorAll(selector));
    // console.log(elements,selector)；
    this.length = elements.length ? elements.length : 0;
    elements.forEach((item, i) => {
      this[i] = item;
    });
  }
  html(htmlText:String){
    if (htmlText) {
        //如果有值就是赋值
      for (var i = 0; i < this.length; i++){
        this[i].innerHTML = htmlText;
      };
      console.log('此时是html方法赋值状态');
    } else {
      //如果没有值就是取值
      console.log('此时是html方法取值状态');
      return this[0];
      }
  }
};
window.$('.box').html('000999');


// 总结：简单工厂模式就是函数里返回类的实例
